/* eslint-disable no-unused-vars */
<template>
  <div>
    <form style="display: flex;justify-content: center;margin-top: 150px">
      <el-card style="width: 400px">
        <div slot="header" class="clearfix">
          <span>登录</span>
        </div>
        <table>
          <tr>
            <td>用户名</td>
            <td>
              <el-input
                v-model="user.username"
                placeholder="请输入用户名"
                required
              ></el-input>
            </td>
          </tr>
          <tr>
            <td>密码</td>
            <td>
              <el-input
                type="password"
                v-model="user.password"
                placeholder="请输入密码"
                @keydown.enter.native="doLogin"
              ></el-input>
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <el-button style="width: 300px" type="primary" @click="doLogin"
                >登录</el-button
              >
            </td>
          </tr>
        </table>
      </el-card>
    </form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      user: {
        username: "admin",
        password: "123456"
      }
    }
  },
  methods: {
    doLogin() {
      //一点击登录按钮，这个方法就会执行
      // eslint-disable-next-line no-unused-vars
      if (this.user.username !== "" && this.user.password !== "") {
        let newParams = {}
        newParams = new FormData()
        newParams.append("userId", this.user.username)
        newParams.append("password", this.user.password)
        this.$Http
          .userLogin(newParams)
          .then(res => {
            let data = res.data
            if (data.status != "") {
              this.$message.success("登录成功")
              let orgId = res.data.data.organization.orgId //得到了机构的id,orgId
              this.$store.commit("setOrgId", orgId) //设置orgId的状态
              let token = this.user
              token.sysRouters = [
                "/login",
                "/home",
                "/staffmanage",
                "/stumanage",
                "/videowall",
                "/videoreplay",
                "/channelconfig",
                "/warnlist",
                "/Comeout",
                "/Carout",
                "/Carwhite",
                "/Equipment",
                "/Equipmentclass",
                "/EmergencyPlan",
                "/HiddenDangerList",
                "/PatrolPlan",
                "/OrganizationManagement",
                "/UserManagement",
                "/RoleManagement"
              ]
              token.authRouters = [
                "/login",
                "/home",
                "/staffmanage",
                "/stumanage",
                "/UserManagement",
                "/RoleManagement"
              ]
              sessionStorage.setItem("token", JSON.stringify(token))
              this.$router.push({
                name: "Home",
                params: {
                  orgId: orgId
                }
              })
            } else {
              this.$message.warning("登录失败，用户名和密码错误")
            }
          })
          .catch(err => {})
      } else {
        this.$message.warning("请输入有效的用户名和密码")
      }
    }
  }
}
</script>
